﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using net8.Models.XiTong;
using net8.Models;
using net8.Views;
using net8.Views.XiTong;
using System.Drawing.Printing;

namespace net8.Controllers.XiTong
{
    /// <summary>
    /// 公司管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class XtCompanyController : ControllerBase
    {
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="model">分页模型</param>
        /// <returns></returns>
        [HttpGet]
        public PageReturn<XtCompany> GetPageList([FromQuery] CompanyQuery model)
        {
            int total = 0;
            var list = DbHelper.Db.Queryable<XtCompany>()
                .WhereIF(!string.IsNullOrEmpty(model.CompanyCode), c => c.CompanyCode.Contains(model.CompanyCode))
                .WhereIF(!string.IsNullOrEmpty(model.CompanyName), c => c.CompanyName.Contains(model.CompanyName))
                .WhereIF(!string.IsNullOrEmpty(model.MobilePhone), c => c.MobilePhone.Contains(model.MobilePhone))
                .ToPageList(model.PageIndex, model.PageSize, ref total);
            return new PageReturn<XtCompany>() { Total = total, List = list };
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        public void Save(XtCompany model)
        {
            DbHelper.Db.Storageable(model).ExecuteCommand();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id">主键</param>
        [HttpDelete]
        public void Delete(Guid id)
        {
            DbHelper.Db.Deleteable<XtCompany>(id).ExecuteCommand();
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id">主键</param>
        [HttpPost]
        public void State([FromBody] XtCompany model)
        {
            XtCompany entity = DbHelper.Db.Queryable<XtCompany>().First(c => c.Id == model.Id);
            entity.IsDel = !entity.IsDel;
            DbHelper.Db.Updateable(entity).ExecuteCommand();
        }

        /// <summary>
        /// 获取企业下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<TreeSelect> GetCompanySelectList()
        {
            var list = DbHelper.Db.Queryable<XtCompany>()
                .Select(c => new TreeSelect()
                {
                    Label = c.CompanyName,
                    Value = c.Id
                }).ToList();
            return list;
        }
    }

    /// <summary>
    /// 公司分页参数
    /// </summary>
    public class CompanyQuery : PageQuery
    {
        /// <summary>
        /// 统一社会信用代码
        /// </summary>
        public string? CompanyCode { get; set; }
        public string? CompanyName { get; set; }
        public string? MobilePhone { get; set; }
    }
}
